using System;
using System.Net.Sockets;

public class SocketClient
{
    private readonly byte[] buffer = new byte[1024];
    private int receivedDataLength;
    private Socket socket;

    public void Connect(string ip, int port)
    {
        socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        socket.BeginConnect(ip, port, ConnectCallback, null);
    }

    private void ConnectCallback(IAsyncResult ar)
    {
        socket.EndConnect(ar);
        socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, ReceiveCallback, null);
    }

    private void ReceiveCallback(IAsyncResult ar)
    {
        receivedDataLength = socket.EndReceive(ar);
        var data = new byte[receivedDataLength];
        Array.Copy(buffer, data, receivedDataLength);

        // Process the complete message here

        socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, ReceiveCallback, null);
    }
}